package com.etl.dataflow.scheduler.timewheel;

import com.etl.dataflow.scheduler.cron.CronExpression;

/**
 * 时间轮 门面
 *
 * @author dx
 * @since 2022/6/17
 */
public class TimeWheelFacade {
    private static volatile TimeWheelFacade INSTANCE = null;
    private final TimeWheel wheel;

    private TimeWheelFacade() {
        TimeWheel yearWheel = new TimeWheel(WheelType.YEAR_WHEEL, 12);
        TimeWheel monthWheel = new TimeWheel(WheelType.MONTH_WHEEL, 31);
        TimeWheel dayWheel = new TimeWheel(WheelType.DAY_WHEEL, (24 * 3600));
        yearWheel.setChild(monthWheel);
        monthWheel.setParent(yearWheel);
        monthWheel.setChild(dayWheel);
        dayWheel.setParent(monthWheel);
        wheel = dayWheel;
    }

    public static TimeWheelFacade getInstance() {
        if (INSTANCE == null) {
            synchronized (TimeWheelFacade.class) {
                if (INSTANCE == null) {
                    INSTANCE = new TimeWheelFacade();
                }
            }
        }
        return INSTANCE;
    }

    public void addTask(CronExpression cronExpression, WheelTask wheelTask) {
        this.wheel.addTask(cronExpression, wheelTask);
    }

    public void removeTask(String wheelTaskId) {
        this.wheel.removeTask(wheelTaskId);
    }
}
